<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				margin: 100px auto;
				width: 640px;
				height: 320px;
				position: relative;
				overflow: hidden;
				/*border: 1px solid blue;*/
			}
			
			.container .imgs {
				position: absolute;
				width: 3840px;
				height: 320px;
				left: 0px;
				top: 0px;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<div class="imgs">
				<img src="imgfq/img1.jpg" /><img src="imgfq/img2.jpg" /><img src="imgfq/img3.jpg" /><img src="imgfq/img4.jpg" /><img src="imgfq/img5.jpg" /><img src="imgfq/img1.jpg" />
			</div>
		</div>
		<input type="button" id="left" value="left" />
		<input type="button" id="right" value="right" />
	</body>
	<script type="text/javascript">
		var imgs = document.querySelector(".container .imgs");
		var imgsCount = document.querySelectorAll(".container .imgs>img").length;
		var distance = 0;
		var leftMaxDistance = parseInt(getComputedStyle(imgs).width) / imgsCount * (imgsCount - 1);
		var left = document.querySelector("#left");
		var right = document.querySelector("#right");
		var leftRun;
		var rightRun;
		left.onclick = function() {
			//先关再开
			clearInterval(rightRun);
			runLeft();
		};
		right.onclick = function() {
			clearInterval(leftRun);
			runRight();
			
		};
		function runLeft() {
			//先关再开
			clearInterval(leftRun);
			leftRun = setInterval(function() {
				if (distance <= -leftMaxDistance) {
					distance = 0;
				}
				imgs.style.left = (distance = distance - 10) + "px";
			}, 50);
		}

		function runRight() {
			clearInterval(rightRun);
			rightRun = setInterval(function() {
				if (distance >= 0) {
					distance = -leftMaxDistance;
				}
				imgs.style.left = (distance = distance + 10) + "px";
			}, 50);
		}
	</script>
</html>